# 通用设施文件
import functools

from flask import session, jsonify, g
from werkzeug.routing import BaseConverter

from i_Home.utils.response_code import RET


class RegexConverter(BaseConverter):
    """自定义正则转换器"""

    def __init__(self, url_map, *args):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]


# 定义一个装饰器来判断用户是否登陆
def login_required(func):
    @functools.wraps(func)
    def wrapper(*args,**kwargs):
        user_id = session.get('user_id')
        if user_id is None:
            print('用户未登录')
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
        else:
            g.user_id = user_id
            return func(*args,**kwargs)
    return wrapper